package com.baselibrary.view.refresh_header;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.baselibrary.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * @author: 关忆北、
 * @date: 2022/6/22
 * @Des：
 */
public class HKRefreshHeaderView extends RelativeLayout implements RefreshHeader {
    public static final String REFRESH_HEADER_DOWN_TO_TRFRESH = "下拉可以刷新";
    public static final String REFRESH_HEADER_DOWN_CANCEL = "下拉取消";
    public static final String REFRESH_HEADER_RELEASE = "释放立即刷新";
    public static final String REFRESH_HEADER_LOADING = "正在加载...";
    public static final String REFRESH_HEADER_FINISH = "刷新成功";
    public static final String REFRESH_HEADER_FAILED = "刷新失败";
    private RingProgressBar mCirCulBar;
    private RelativeLayout mRlRefresh;
    private float signDownDistance;//记录下啦距离
    private float signDownDistance_Y;
    private float signUpDistance_Y;
    private float signMoveDistance_Y;
    private LinearLayout.LayoutParams layoutParams;
    private static final int HANDLER_CODE = 0x11;
    private Handler tHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (mCirCulBar.getProgress() >= mCirCulBar.getMax()) {
                mCirCulBar.setProgress(5);
            } else {
                mCirCulBar.setProgress(mCirCulBar.getProgress() + 5);
            }
            sendDelayedEmptyMes();
            return false;
        }
    });

    public HKRefreshHeaderView(Context context) {
        super(context);
        initView();
    }

    public HKRefreshHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HKRefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View mViewLoading = LayoutInflater.from(getContext()).inflate(R.layout.view_loading, this);
        mCirCulBar = mViewLoading.findViewById(R.id.cir_cul_bar);
        mRlRefresh = mViewLoading.findViewById(R.id.rl_refresh);
        layoutParams = (LinearLayout.LayoutParams) mRlRefresh.getLayoutParams();
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    /*
        【仅限框架内调用】手指拖动下拉（会连续多次调用，添加isDragging并取代之前的onPulling、onReleasing）
        Params:
        isDragging – true 手指正在拖动 false 回弹动画
        percent – 下拉的百分比 值 = offset/footerHeight (0 - percent - (footerHeight+maxDragHeight) / footerHeight )
        offset – 下拉的像素偏移量 0 - offset - (footerHeight+maxDragHeight)
        height – 高度 HeaderHeight or FooterHeight (offset 可以超过 height 此时 percent 大于 1)
        maxDragHeight – 最大拖动高度 offset 可以超过 height 参数 但是不会超过 maxDragHeight*/
    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        mCirCulBar.setMax(height);
        mCirCulBar.setProgress(offset);
        /*layoutParams.topMargin = ((int) (percent * 100) - ScreenUtils.dip2px(getContext(), 20f));
        mRlRefresh.setLayoutParams(layoutParams);*/
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        tHandler.removeMessages(HANDLER_CODE);
        if (success) {
            //刷新完成
            // mTitleTv.setText(REFRESH_HEADER_FINISH);
        } else {
            //刷新失败
        }

        // mCirCulBar.setVisibility(View.GONE);
        return 500;//延迟500毫秒之后再弹回
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }


    @Override

    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case PullDownToRefresh:
                //mTitleTv.setText(REFRESH_HEADER_DOWN_TO_TRFRESH);
                break;
            case PullDownCanceled:
                //mTitleTv.setText(REFRESH_HEADER_DOWN_CANCEL);
                break;
            case ReleaseToRefresh:
                //mTitleTv.setText(REFRESH_HEADER_RELEASE);
                break;
            case Refreshing:
                //正在刷新
                //mTitleTv.setText(REFRESH_HEADER_LOADING);
                mCirCulBar.setProgress(0);
                sendDelayedEmptyMes();
                break;
        }
    }

    private void sendDelayedEmptyMes() {
        tHandler.removeMessages(HANDLER_CODE);
        tHandler.sendEmptyMessageDelayed(HANDLER_CODE, 10);
    }
}
